//
//  FR_Feedback_ViewController.m
//  FaceRecognition
//
//  Created by Sylar on 2017/8/8.
//  Copyright © 2017年 Sylar. All rights reserved.
//

#import "FR_Feedback_ViewController.h"

@interface FR_Feedback_ViewController ()<UITextViewDelegate>
{
    UILabel *textViewPlaceholderLabel;
    UIButton *submitBtn;
}

@property (nonatomic, strong) UITextView *myTextView;


@end

@implementation FR_Feedback_ViewController

- (void)loadView {
    
    [super loadView];
    
    self.navBar.nav_titleLabel.text = @"问题反馈";
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    textViewPlaceholderLabel = [[UILabel alloc] initWithFrame:CGRectMake(18, 24+SafeAreaTopHeight, 280, 25)];
    [textViewPlaceholderLabel setFont:[UIFont systemFontOfSize:16]];
    textViewPlaceholderLabel.text = @"请输入您的意见，我们将不断改进";
    textViewPlaceholderLabel.textColor = [UIColor grayColor];
    
    [self.view addSubview:self.myTextView];
    [self.view addSubview:textViewPlaceholderLabel];
    
    submitBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [submitBtn setFrame:CGRectMake(15, self.myTextView.bottom+10, SCREENWIDTH-30, 40)];
    [submitBtn setBackgroundColor:colorFromHex(@"b4e0ff")];
    [submitBtn setTitle:@"提交" forState:(UIControlStateNormal)];
    [submitBtn setTitleColor:colorFromHex(@"ffffff") forState:(UIControlStateNormal)];
    submitBtn.layer.cornerRadius = 4.0;
    [submitBtn addTarget:self action:@selector(submit) forControlEvents:(UIControlEventTouchUpInside)];
    submitBtn.enabled = NO;
    
    [self.view addSubview:submitBtn];
    
    [self.myTextView becomeFirstResponder];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)submit {
    
    [self.view endEditing:YES];
    
    NSMutableDictionary * dict = [[NSMutableDictionary alloc] initWithCapacity:0];
    [dict setObject:@"api_feedback_add" forKey:@"method"];
    [dict setObject:@"20" forKey:@"tittle"];
    [dict setObject:USERID forKey:@"userid"];
    [dict setObject:self.myTextView.text forKey:@"scontent"];
    [dict setObject:@"1" forKey:@"type"];
    
    [Utils get:nil parameters:dict success:^(NSInteger recode, id resposeJson){
        
        if (recode == 0) {
            
            [self showHint:@"提交成功，感谢您的反馈"];
            
            [self.navigationController popViewControllerAnimated:YES];
        }
        
        
    } failure:^(NSInteger recode, NSError * error){
        
    }];
    
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    //[text isEqualToString:@""] 表示输入的是退格键
    if (![text isEqualToString:@""]) {
        
        textViewPlaceholderLabel.hidden = YES;
        
    }
    
    //range.location == 0 && range.length == 1 表示输入的是第一个字符
    if ([text isEqualToString:@""] && range.location == 0 && range.length == 1) {
        textViewPlaceholderLabel.hidden = NO;
        
    }
    
    return YES;
    
}

- (void)textViewDidChange:(UITextView *)textView {
    
    if (![self isEmpty:textView.text]) {
        [submitBtn setBackgroundColor:DEFAULTCOLOR];
        submitBtn.enabled = YES;
    }else {
        [submitBtn setBackgroundColor:colorFromHex(@"b4e0ff")];
        submitBtn.enabled = NO;
    }
    
    if (textView.text.length > 150) {
        
        textView.text = [textView.text substringToIndex:150];
    }
}

- (BOOL) isEmpty:(NSString *) str {
    if (!str) {
        return true;
    } else {
        NSCharacterSet *set = [NSCharacterSet whitespaceAndNewlineCharacterSet];
        NSString *trimedString = [str stringByTrimmingCharactersInSet:set];
        if ([trimedString length] == 0) {
            return true;
        } else {
            return false;
        }
    }
}

#pragma mark - Get

- (UITextView *)myTextView {
    
    if (_myTextView == nil) {
        
        _myTextView = [[UITextView alloc]initWithFrame:CGRectMake(15, 20+SafeAreaTopHeight, SCREENWIDTH-30, 150)];
        [_myTextView setFont:[UIFont systemFontOfSize:16]];
        [_myTextView setTextColor:colorFromHex(@"3b3b3b")];
        _myTextView.delegate = self;
        
    }
    
    return _myTextView;
}

@end
